package com.teamwings.modules.design.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.exception.ServiceException;
import com.teamwings.ctrls.IField;
import com.teamwings.ctrls.entity.DatabaseTable;
import com.teamwings.ctrls.entity.DatabaseTableWithColumns;
import com.teamwings.modules.code.entity.FormTableCode;
import com.teamwings.modules.code.service.IFormTableCodeService;
import com.teamwings.common.aspect.FormControlMap;
import com.teamwings.common.exception.ResultException;
import com.teamwings.ctrls.Field;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.entity.FormTableDesignTemplate;
import com.teamwings.modules.design.mapper.DesignMapper;
import com.teamwings.modules.design.service.IDesignService;
import com.teamwings.modules.design.service.IDesignTemplateService;
import com.teamwings.modules.design.vo.FormTableVo;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.head.entity.FormTableHead;
import com.teamwings.modules.head.service.IFormTableHeadService;
import com.teamwings.modules.page.service.IPageService;
import com.teamwings.util.FieldsLoopUtil;
import com.teamwings.util.VoUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.TriConsumer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 表单设计服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-05-06
 */
@Service
@Slf4j
public class DesignServiceImpl implements IDesignService {

    @Autowired
    private IFormTableHeadService formTableHeadService;

    @Autowired
    private IFormTableCodeService formTableCodeService;

    @Autowired
    private DesignMapper designMapper;

    @Autowired
    private IPageService pageService;

    @Autowired
    private IDesignTemplateService designTemplateService;

    @CacheEvict(value = {
            // FormConstant.FORM_DESIGN_PAGING,
            FormConstant.FORM_HEAD_PAGING
    }, key = "'*'")
    @Override
//    @Transactional
    public void save(String jsonFormVo, FormTableHead head, String tableRemarks) {
        FormVo formVo = VoUtils.newFormVo(jsonFormVo);
        String tableHeadId = "";
        String tableName = "";
        FormTableHead formHeadEntity = null;

        // 当前已经不需要在这里创建 tableHead, 由 FormTableHeadServiceImpl 创建
        if (StringUtils.isEmpty(formVo.getTableHeadId())) {
            tableName = this.generateTableName(formVo);

            // 保存FormTableHeader
            formHeadEntity = new FormTableHead();
            formHeadEntity.setTableName(tableName);
            formHeadEntity.setTableTxt(formVo.getTableTxt());
            formHeadEntity.setTableCateId(formVo.getTableCateId());
            formHeadEntity.setTableVersion(1);
            formHeadEntity.setDeleted(0);
            formHeadEntity.setTableCname(formVo.getTableCname());

            formTableHeadService.save(formHeadEntity);
            tableHeadId = formHeadEntity.getId();
        } else {

            tableHeadId = formVo.getTableHeadId();
            // formHeadEntity = formTableHeadService.getById(tableHeadId);
            formHeadEntity = head;
            // formHeadEntity.setTableVersion(1);
            tableName = formHeadEntity.getTableName();
        }

        // if (formHeadEntity.getDeleted() == null) {
        //     formHeadEntity.setDeleted(0);
        // }

        //String tableCode = JSON.toJSONString(formVo);
        String tableCode = jsonFormVo;
        FormTableCode formCodeEntity = new FormTableCode();
        formCodeEntity.setTableSource(head.getSourceType() + "");
        formCodeEntity.setTableSourceType("0"); // 暂时由自己管理，无论是否表单自己创建表
        formCodeEntity.setTableCode(tableCode);
        formCodeEntity.setTableHeadId(tableHeadId);
        formCodeEntity.setTableVersion(1);
        formCodeEntity.setRemarks(tableRemarks);

        // 创建数据表
        List<FormComponent> filedList = formVo.getList();
        List<String> statementList = new ArrayList<>();
        List<String> fieldsList = new ArrayList<>();



        // 遍历控件
        FieldsLoopUtil.loop(filedList, (FormComponent component) -> {
            String statement = component.getStatement(true);
            if (statement.equals("")) {
                return;
            }
            Field field = component.newField();
            // 记录需要添加的SQL语句，以及字段名称List
            if (field != null) {
                String fieldName = field.getFieldName();
                statementList.add(statement);
                fieldsList.add(fieldName);
            }

            // 进行内部字段添加
            component.handleInternalFieldAdding(statementList, fieldsList);
        });

        // 保存FormTableCode对象
        formCodeEntity.setTableFields(StringUtils.join(fieldsList, ","));
        formTableCodeService.save(formCodeEntity);

        // 是否建表，当前sourceType=0为需要数据源，需要创建表
        //因为有可能创建后直接点击关闭，所以去除组件判断
        //if (formHeadEntity.getSourceType() == 0 && fieldsList.size() > 0) {
        if (formHeadEntity.getSourceType() == 0) {
            // 2021-08-23 需要限制一下表格名称的前缀，避免与其它表冲突
            if (!tableName.startsWith("form_user_")) {
                if (tableName.startsWith("FORM_USER_")) {

                } else {
                    tableName = "form_user_" + tableName;
                }
            }
            // TODO 需要检查是否存在同名的表
            designMapper.createTable(tableName, statementList);
        }

        // 更新form_table_head的表名
        // formHeadEntity.setTableName(tableName);
        // formTableHeadService.updateById(formHeadEntity);
    }

    // 根据公共控件对象 + classname获取子控件对象同时用一个新的公共控件对象接收这个子控件
    private FormComponent getFormComponentByClassname(FormComponent formComponent, String classname) {
        Class<?> aClass = null;
        try {
            aClass = Class.forName(classname);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        FormComponent component = null;
        try {
            if (aClass != null) {
                component = (FormComponent) aClass.getDeclaredConstructor().newInstance();
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            e.printStackTrace();
        }

        if (component == null) {
            // todo 如果没有实例：1.有可能控件type在后台中不存在 2.其他情况...
        }
        /*
            对象拷贝
                formComponent是客户端传过来的表单控件，
                component是后端根据客户端传过来的类型"type"实例化的对象
         */
        BeanUtils.copyProperties(formComponent, component);
        return component;
    }

    /**
     * 生成表名
     * 如果用户设置了，按照用户的表名建表。否则系统自动生成
     *
     * @param formVo
     * @return
     */
    private String generateTableName(FormVo formVo) {
        String tableName = "";

        if (StringUtils.isEmpty(formVo.getTableName())) {
            // 系统自动生成表名
            tableName = "form_user_" + IdWorker.getId();
        } else {
            // 用户自定义表名
            tableName = "form_user_" + formVo.getTableName();
            // 判断数据库是否存在该表
            Boolean hasTable = designMapper.hasTable(tableName);
            if (hasTable) {
                throw new ResultException("表名已存在");
            }
        }
        return tableName;
    }

    private String generateTableName() {
        // 系统自动生成表名
        String tableName = "form_user_" + System.currentTimeMillis();
        // 判断数据库是否存在该表
        Boolean hasTable = designMapper.hasTable(tableName);
        if (hasTable) {
            throw new ResultException("表名已存在");
        }
        return tableName;
    }


    @CacheEvict(value = {
            // FormConstant.FORM_DESIGN_PAGING,
            FormConstant.FORM_HEAD_PAGING},
            key = "'*'")
    @Override
    @Transactional
    public void edit(String jsonFormVo, String tableRemarks) {
        // 无数据来源通用表名
        String noSourceTableName = "/";
        FormVo formVo = VoUtils.newFormVo(jsonFormVo);

        if (StringUtils.isEmpty(formVo.getTableHeadId())) {
            throw new ResultException("表单设计异常：tableHeadId不能为空");
        }
        String tableHeadId = formVo.getTableHeadId();
        Integer tableVersion = StringUtils.isNotEmpty(formVo.getTableVersion()) ? Integer.parseInt(formVo.getTableVersion()) : null;
        //FormTableCode tableCode ;
        FormTableCode formCodeEntity = formTableCodeService.getByFormIdAndVersion(tableHeadId, tableVersion);

        // 判断是否为添加新版本，是
        if ("false".equals(formVo.getIsOld())) {
            // 获取最新的code版本
            FormTableCode latestTableCode = (FormTableCode) formTableCodeService.getAllOrSpecific(tableHeadId, true, "TABLE_VERSION");
            //保存旧的 TableFields 供下面的判断使用，最后保存时再设置新的 TableFields
            String oldTableFields = formCodeEntity.getTableFields();
            String tableSource = formCodeEntity.getTableSource();
            String tableSourceType = formCodeEntity.getTableSourceType();

            // 插入一条新的FormTableCode数据
            formCodeEntity = new FormTableCode();
            formCodeEntity.setTableSource(tableSource);
            formCodeEntity.setTableSourceType(tableSourceType);

            formCodeEntity.setTableHeadId(tableHeadId);
            formCodeEntity.setTableFields(oldTableFields);
            formCodeEntity.setTableVersion(tableVersion = latestTableCode.getTableVersion() + 1);
        }
        // 查询数据库中最新的一个tableCode
            /*修改了逻辑 创建head的时候 直接创建code 所以只会做自改操作
            tableCode = formTableCodeService.getLatestOneByFormId(tableHeadId);

            if (tableCode == null && formVo.getList() != null && formVo.getList().size() > 0) {
                save(jsonFormVo);
                return;
            }
            tableVersion = tableCode.getTableVersion() + 1;
            // 插入一条新的FormTableCode数据
            formCodeEntity = new FormTableCode();
            formCodeEntity.setTableHeadId(tableHeadId);
            formCodeEntity.setTableVersion(tableVersion);
            */

        //formCodeEntity.setTableCode(JSON.toJSONString(formVo));
        formCodeEntity.setTableCode(jsonFormVo);
        formCodeEntity.setRemarks(tableRemarks);


        // 修改FormTableHead数据
        FormTableHead formTableHead = formTableHeadService.getById(tableHeadId);
        String tableName = formTableHead.getTableName();
        // 如果是保存新版本，则修改 如果传入的 code里的 和 当前的版本不一样 则不修改当前版本
        if ("false".equals(formVo.getIsOld()) || tableVersion.equals(formTableHead.getTableVersion())) {
            BeanUtils.copyProperties(formVo, formTableHead);
            formTableHead.setTableVersion(tableVersion);
            formTableHead.setUpdateTime(new Date());
            formTableHeadService.updateById(formTableHead);
        }

        // 无数据来源不建表不需要编辑字段操作
        if (!noSourceTableName.equals(tableName)) {

            List<String> originFields = null;
            if (StringUtils.isEmpty(formCodeEntity.getTableFields())) {
                originFields = new ArrayList<>();
            } else {
                originFields = Lists.newArrayList(formCodeEntity.getTableFields().split(","));
            }

//        List<String> fieldsList = new ArrayList<>();
            Set<String> fieldsList = new HashSet<>();
//        fieldsList.addAll(originFields);
            // 修改后
            List<FormComponent> formComponents = formVo.getList();

            Boolean aBoolean = designMapper.hasTable(tableName);
            if (!aBoolean) {
                // 表不存在
                designMapper.createTable(tableName, new ArrayList<>());
            }

            List<String> finalOriginFields = originFields;

            // 控件内部子字段
            // 定义 检查控件内部字段匿名函数
            TriConsumer<String, FormComponent, IField> deisgnMapperHandler = (name, comp, field) -> {
                // if (!finalOriginFields.contains(name)) {
                boolean bContain = finalOriginFields.stream().anyMatch(name::equalsIgnoreCase);
                if (!bContain) {
                    designMapper.addColumn(tableName, comp.getStatement(true, field));
                } else {
                    designMapper.editColumnType(tableName, comp.getStatement(false, field));
                }
            };

            // 遍历所有控件
            // 记录字段名，以及执行添加字段的数据库操作
            FieldsLoopUtil.loop(formComponents, (FormComponent component) -> {
                //String classname = FormControlMap.typeClassMap.get(component.getType());
                //if (StringUtils.isNotEmpty(classname)) {
                String key = component.getKey();
                String model = StringUtils.isNotEmpty(component.getModel())
                        ? component.getModel()
                        : key;
                // 原表的字段列表中包含当前字段： 可能会触发对当前字段进行属性修改
                Field field = component.newField();
                if (field == null) { return; }

                String fieldName = field.getFieldName();
                fieldsList.add(fieldName);


                // TODO 修改前后表单的类型比对，如果一样就不修改

                boolean bContain = finalOriginFields.stream().anyMatch(model::equalsIgnoreCase);
                // if (finalOriginFields.contains(model)) {
                if (bContain) {
                    String statement = component.getStatement(false);
                    designMapper.editColumnType(tableName, statement);
                } else {
                    String statement = component.getStatement(true);
                    designMapper.addColumn(tableName, statement);
                }

                // 检查控件内部字段
                component.handleInternalFieldEditing(deisgnMapperHandler, fieldsList);
            });

            // 无数据来源不建表不需要增加字段操作
            // “原表的字段”和“当前表的字段”求差集 =》 要删除字段
            originFields.removeAll(fieldsList);
            // 判断表中是否有数据
            Boolean hasData = designMapper.hasData(tableName);
            if (!hasData) {
                for (String column : originFields) {
                    designMapper.dropColumn(tableName, column.toLowerCase(Locale.ROOT));
                }
            }
            formCodeEntity.setTableFields(StringUtils.join(fieldsList, ","));
        }
        formCodeEntity.setUpdateTime(new Date());
        formTableCodeService.saveOrUpdate(formCodeEntity);

//        Map<String, String> layoutTypeClassMap = FormControlMap.layoutTypeClassMap;
//        // 获取所有布局控件的key
//        Set<String> layoutTypeList = layoutTypeClassMap.keySet();
//        // 记录字段
//        List<String> fieldsList = new ArrayList<>();
//
//        for (FormComponent formComponent : formComponents) {
//            String type = formComponent.getType();
//            // 控件类型判断 【控件类型分为：普通控件，布局控件。 他们利用自定以注解@FormControl的isLayout属性进行区分】
//            if (layoutTypeList.contains(type)) {
//                String layoutType = layoutTypeClassMap.get(type);
//                // 实例化布局控件
//                FormComponent layoutFormComponent = this.getFormComponentByClassname(formComponent, layoutType);
//                // 获取布局里面的普通控件列表
//                List<FormComponent> componentsInTheLayout = layoutFormComponent.getFormComponentList();
//
//                for (FormComponent componentInTheLayout : componentsInTheLayout) {
//                    // 处理布局控件中的普通控件
//                    String componentTypeInTheLayout = componentInTheLayout.getType();
//                    doEditComponent(tableName, fieldsList, originFields, componentInTheLayout, componentTypeInTheLayout);
//                }
//            } else {
//                // 处理普通控件
//                doEditComponent(tableName, fieldsList, originFields, formComponent, type);
//            }
//        }
//
//        // 保存FormTableCode对象
//        formCodeEntity.setTableFields(StringUtils.join(fieldsList, ","));
//        formTableCodeService.save(formCodeEntity);
    }

    //    @Cacheable(value="designPage", key="#page+'_'+#pageSize")
    @Override
    public IPage<FormVo> page(Long page, Long pageSize, String key, String cateId) {
        Page<FormTableHead> headPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<FormTableHead> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(cateId)) {
            queryWrapper.eq(FormTableHead::getTableCateId, cateId);
        }
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.eq(FormTableHead::getTableName, key);
        }

        // 获取FormTableHead的分页
        IPage<FormTableHead> headPageData = formTableHeadService.page(headPage, queryWrapper);

        // 拿到里面的分页数据，利用Head分页数据寻找完整的分页数据
        List<FormTableHead> records = headPageData.getRecords();
        // 拿到所有headIds
        List<String> ids = records.stream().map(FormTableHead::getId).collect(Collectors.toList());
        // 利用headIds查询对应的FormTableCode. (getLatestListByFormIds方法是利用table_version和table_head_id来进行查询的)
        List<FormTableCode> FormTableCodeList = formTableCodeService.getLatestListByFormIds(ids);
        // 拿到所有FormTableCode数据，打包成key为head_id,value为table_code的map数据结构
        Map<String, String> headIdAndCodeMap = FormTableCodeList.stream()
                .collect(Collectors.toMap(FormTableCode::getTableHeadId, FormTableCode::getTableCode));

        List<FormVo> formVoRecords = new ArrayList<>();
        for (FormTableHead formTableHead : records) {
            String headId = formTableHead.getId();
            // 根据head_id找到对应的tableCode数据
            String jsonString = headIdAndCodeMap.get(headId);
            if (StringUtils.isNotEmpty(jsonString)) {
                //FormVo formVo = JSON.parseObject(jsonString, FormVo.class);
                FormVo formVo = VoUtils.newFormVo(jsonString);
                BeanUtils.copyProperties(formTableHead, formVo);
                formVoRecords.add(formVo);
            }
        }

        // 组装FormVo分页数据
        IPage<FormVo> formPageData = new Page<>();
        formPageData.setTotal(headPageData.getTotal());
        formPageData.setCurrent(headPageData.getCurrent());
        formPageData.setSize(headPageData.getSize());
        formPageData.setRecords(formVoRecords);
        return formPageData;
    }


    //    @CacheEvict(value = "designPage", allEntries = true)
    @Override
    public void cloneForm(String tableHeadId, String newTableName) {
        if (StringUtils.isEmpty(tableHeadId)) {
            throw new ResultException("tableHeadId不能为空");
        }

        if (StringUtils.isEmpty(newTableName)) {
            // 自动生成一个表名
            newTableName = this.generateTableName();
        }

        Boolean hasTable = formTableHeadService.hasTable(newTableName);
        if (hasTable) {
            throw new ResultException("该表名已存在");
        }

        FormTableHead originEntity = formTableHeadService.getById(tableHeadId);
        FormTableCode latestTableCode = formTableCodeService.getLatestOneByFormId(originEntity.getId());

        try {
            // 数据表克隆
            designMapper.cloneTable(originEntity.getTableName(), newTableName);
        } catch (Exception e) {
            throw new ServiceException("数据表克隆失败");
        }

        // 拷贝form_table_head表的对应数据
        FormTableHead formTableHead = new FormTableHead();
        formTableHead.setTableName(newTableName);
        formTableHead.setTableVersion(1);
        formTableHead.setTableTxt(originEntity.getTableTxt());
        formTableHead.setTableCateId(originEntity.getTableCateId());
        String tableCname = originEntity.getTableCname();
        if (StringUtils.isNotEmpty(tableCname)) {
            formTableHead.setTableCname(tableCname + "_copy");
        }
        formTableHead.setDeleted(0);
        formTableHeadService.save(formTableHead);

        // 拷贝form_table_code表的对应数据
        FormTableCode formTableCode = new FormTableCode();
        formTableCode.setTableHeadId(formTableHead.getId());
        formTableCode.setTableCode(latestTableCode.getTableCode());
        formTableCode.setTableFields(latestTableCode.getTableFields());
        formTableCode.setTableVersion(1);
        formTableCode.setTableSource(latestTableCode.getTableSource());
        formTableCode.setTableSourceType(latestTableCode.getTableSourceType());
        formTableCodeService.save(formTableCode);
    }

    @Override
    public List<FormTableVo> getLatestListByFormIds(List<String> headIds) {
        return designMapper.getLatestListByFormIds(headIds);
    }

    @Override
    public List<FormTableVo> getLatestList() {
        return this.getLatestListByFormIds(null);
    }

    @Override
    public List<DatabaseTable> allTable() {
        return designMapper.allTable();
    }

    @Override
    public List<DatabaseTableWithColumns> allTableWithColumns() {
        return designMapper.allTableWithColumns();
    }

    @Override
    public DatabaseTableWithColumns getTableWithColumns(String tableName) {
        return designMapper.getTableWithColumns(tableName);
    }

    @Deprecated
    private void doEditComponent(String tableName, List<String> fieldsList, List<String> originFields, FormComponent formComponent, String type) {
        String key = formComponent.getKey().toUpperCase();
        String model = formComponent.getModel().toUpperCase();

        /*
            todo 此处还可以做列的删除，这里先不做处理...
                1. 前端可以用某些字段标记该列需要删除比如：isDelete:true,
                2. 获取通过求差集进行自动删除 （旧表单的key数组 - 新表单的key数组）
         */

        if (originFields.contains(model) || originFields.contains(key)) {
            // 该字段在原本的表单中存在

            // todo 重命名列：前端需要规定一个字段，用于记录原本的列名

            // 修改字段类型
            String classname = FormControlMap.typeClassMap.get(type);
            if (StringUtils.isEmpty(classname)) {
                // todo 遇到系统中不存在的控件
                return;
            }

            FormComponent component = this.getFormComponentByClassname(formComponent, classname);
            //Field field = new Field();
            //field.setIsEditColumnType(true);
            //component.setField(field);

            Field field = component.newField();
            if (field != null) {
                String fieldName = field.getFieldName();
                fieldsList.add(fieldName.toUpperCase());
                // todo 修改前后表单的类型比对，如果一样就不修改

                // 这里暂时先这样处理：直接更新所有交集数据的字段的类型
                // designMapper.editColumnType(tableName, component.getField().getStatement());
                String statement = component.getStatement(false);
                if (statement.equals("") == false) {
                    designMapper.editColumnType(tableName, statement);
                }
            }

        } else {
            // 该字段在原本的表单中不存在
            String classname = FormControlMap.typeClassMap.get(formComponent.getType());
            if (StringUtils.isEmpty(classname)) {
                // todo 遇到系统中不存在的控件
                return;
            }
            FormComponent component = getFormComponentByClassname(formComponent, classname);
            // designMapper.addColumn(tableName, component.getField().getStatement());
            String statement = component.getStatement(true);
            if (statement.equals("") == false) {
                designMapper.addColumn(tableName, statement);
            }

            Field field = component.newField();
            if (field != null) {
                String fieldName = field.getFieldName();
                fieldsList.add(fieldName);
            }
        }
    }

    @Override
    public void createDesignTable(String jsonFormVo, String tableName) {
        FormVo formVo = VoUtils.newFormVo(jsonFormVo);
        // 创建数据表
        List<FormComponent> filedList = formVo.getList();
        List<String> statementList = new ArrayList<>();
        List<String> fieldsList = new ArrayList<>();

        // 这里
        FieldsLoopUtil.loop(filedList, (FormComponent component) -> {
            String statement = component.getStatement(true);
            if (statement.equals("")) {
                return;
            }
            Field field = component.newField();
            if (field != null) {
                String fieldName = field.getFieldName();
                statementList.add(statement);
                fieldsList.add(fieldName);
            }
        });
        // 判断数据库是否存在该表
        Boolean hasTable = designMapper.hasTable(tableName);
        if (hasTable) {
            throw new ResultException("表名已存在");
        }
        designMapper.createTable(tableName, statementList);
    }

    @Override
    public void saveWithTemplate(String jsonFormVo, String name, Boolean forceSave) {
        FormVo formVo = VoUtils.newFormVo(jsonFormVo);

        if (StringUtils.isEmpty(formVo.getTableHeadId())) {
            throw new ResultException("表单设计异常：tableHeadId不能为空");
        }

        LambdaQueryWrapper<FormTableDesignTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormTableDesignTemplate::getName, name);
        FormTableDesignTemplate one = designTemplateService.getOne(wrapper);

        FormTableDesignTemplate entity = new FormTableDesignTemplate();
        entity.setName(name);
        entity.setTableCode(jsonFormVo);

        if (one != null) {
            if (forceSave) {
                designTemplateService.updateById(entity);
                return;
            }
            throw new ResultException("有重名的模板");
        }

        designTemplateService.save(entity);
    }


}
